package com.iting.xmldemo;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.*;


import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author iting Nie
 * @version 1.1
 * @create 2021/12/19
 */
public class BeanProcessDemo implements BeanPostProcessor, BeanFactoryPostProcessor, InstantiationAwareBeanPostProcessor
		, DestructionAwareBeanPostProcessor {

	private String valueDemo;

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("BeanPostProcessor的初始化后增强postProcessAfterInitialization方法执行了");
		Class<?> beanClass = bean.getClass();
		if (beanClass == SpeachDemo.class) {
			Object proxyInstance = Proxy.newProxyInstance(beanClass.getClassLoader(), beanClass.getInterfaces(), new InvocationHandler() {
				@Override
				public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
					Object invoke = null;
					try {
						System.out.println("打印日志:使用前");
						invoke = method.invoke(bean, objects);
						System.out.println("打印日志:使用后");
					} catch (Exception e) {
						System.out.println("打印日志:抛出异常");
					} finally {
						System.out.println("打印日志:finally");
					}
					return invoke;
				}
			});
			return proxyInstance;
		}
		return bean;
	}

	private String demo;

	public BeanProcessDemo() {
		System.out.println("BeanProcessDemo的无参构造执行了");
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("BeanPostProcessor的初始化前增强postProcessBeforeInitialization方法执行了");
		// 将需要处理的bean原样返回
		return bean;
	}


	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("BeanFactoryPostProcessor的postProcessBeanFactory方法执行了");
	}

	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		System.out.println("InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法执行了");
		return null;
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		System.out.println("InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation方法执行了");
		return true;
	}

	@Override
	public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
		System.out.println("InstantiationAwareBeanPostProcessor的postProcessPropertyValues方法执行了");
		// 将属性原样返回
		return pvs;
	}

	@Override
	public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
		System.out.println("DestructionAwareBeanPostProcessor的postProcessBeforeDestruction方法执行了");
	}

	@Override
	public boolean requiresDestruction(Object bean) {
		// 直接返回true，表示所有bean都需要被这个后处理器销毁
		return true;
	}

	public void setDemo(String demo) {
		this.demo = demo;
	}

	public String getDemo() {
		return demo;
	}
}
